const superagent = require('superagent');
const util = require('./checkBilibiliURL')
const QUERY_URL = 'http://api.bilibili.com/x/web-interface/view'
const TEST_URL = 'https://www.bilibili.com/video/asd12Z4y1173G?spm_id_from=333.999.0.0'
const temp = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=44004473_26_oem_dg&wd=b%E7%AB%99%E7%BD%91%E5%9D%80%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F&oq=%25E7%25BD%2591%25E5%259D%2580%2520%25E6%25AD%25A3%25E5%2588%2599%25E8%25A1%25A8%25E8%25BE%25BE%25E5%25BC%258F&rsv_pq=de766abd0009f0a0&rsv_t=fca7LxeleO%2Blg5RBixO8SQTQ723QVfGk2haH9NW1H1XUUEk9JUCRk%2F%2F5f34%2Fdouh7BA2CkUURAZV&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_btype=t&inputT=2084&rsv_sug3=113&rsv_sug1=68&rsv_sug7=100&rsv_sug2=0&rsv_sug4=2084'
function isBilibiliURL(url) {
    if (typeof url !== "string") return;
    let array = url.split('/')
    console.log(array)
    const status = util.checkBilibiliURL(array);
    return status;
}
function getVideoInfo(bv) {
    return superagent.get(QUERY_URL).query({ bvid: bv })
}
function interpaceURL(msg) {
    if (msg == undefined || typeof msg !== "string") {
        return false;
    }
    let reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g
    console.log(msg, "msg", typeof msg)
    return msg.match(reg);
}
function formatDate(shijianchuo) {
    function add0(m) { return m < 10 ? '0' + m : m }

    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

module.exports = {
    isBilibiliURL,
    getVideoInfo,
    interpaceURL,
    formatDate
}